package org.xs.web.http;

import java.util.ArrayList;
import java.util.List;

import org.xs.web.http.function.Function0;
import org.xs.web.http.function.Function1;
import org.xs.web.util.SMap;
 
public  class ActionController {

	private List<Function0<Object>> befors = new ArrayList<Function0<Object>>();
	 
	void execBefor(){
		List<Exception> es = new ArrayList<Exception>();
		try {
			for(Function0<Object> func : befors){
				 func.apply();
			}
		} catch (Exception e) {
			es.add(e);
		} 
		if(es.size() > 0){
			throw new BeforExeception(getClass().getSimpleName() + " exec befor func ", es);
		}
	}
	
	protected ActionController befor(Function0<Object> func){
		befors.add(func);
		return this;
	}
	
	/*
	protected void beforValidate(String field,Function1<String, FieldError>[] validateFunc,
			String[] ingore,String[] expect){
		beforValidates.add(new ValidateRule(field, validateFunc, ingore, expect));
	}
	
	protected void beforValidate(String field,Function1<String, FieldError>[] validateFunc,
			String[] ingore){
		beforValidates.add(new ValidateRule(field, validateFunc, ingore, new String[]{}));
	}
	
	protected void beforValidateExpect(String field,Function1<String, FieldError>[] validateFunc,
			String[] expect){
		beforValidates.add(new ValidateRule(field, validateFunc, new String[]{},expect));
	}
	
	protected void beforValidate(String field,Function1<String, FieldError>[] validateFunc){
		beforValidates.add(new ValidateRule(field, validateFunc, new String[]{}, new String[]{}));
	}
	
	protected void beforValidate(String field,Function1<String, FieldError> validateFunc){
		beforValidates.add(new ValidateRule(field, new Function1[]{validateFunc}, new String[]{}, new String[]{}));
	}*/
	
	protected void set(String key,Object value){
		S.set(key, value);
	}
	
	protected SMap<String, Object> params(){
		return S.params();
	}
	
	protected String[] params(String name){
		return S.params(name);
	}
	
	 protected void flash(String value){
		 S.flash().flash(value);
	 }
	
	 protected void flash(String key,String value){
		 S.flash().flash(key,value);
	 }
}
